<?php
class Part extends AppModel {
	var $name = 'Part';
	var $displayField = 'name';
	
	var $validate = array(
		'batch_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'name' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		
	);
	//The Associations below have been created with all possible keys, those that are not needed can be removed

	var $belongsTo = array(
		'Batch' => array(
			'className' => 'Batch',
			'foreignKey' => 'batch_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);

	var $hasMany = array(
		'Question' => array(
			'className' => 'Question',
			'foreignKey' => 'part_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);

	function checkAndCreate($batch, $o_count=7,$e_count=1){
	
		$existingRecordCount = $this->find('count',array('conditions'=>array('Part.batch_id'=>$batch['Batch']['id'])));
		if($existingRecordCount>0) return ;
		$o_count = abs($o_count - $existingRecordCount);	
		for($i=0;$i<$o_count;$i++)
		{
			$this->create();
			$data = array();
			$data['batch_id'] = $batch['Batch']['id'];
			$data['type'] = 'options';
			$data['min_mark'] = '0';
			$data['name'] = 'Part '.($i+1).' Title';
			$this->save($data);
		}
		for($i=0;$i<$e_count;$i++)
		{
			$this->create();
			$data = array();
			$data['batch_id'] = $batch['Batch']['id'];
			$data['type'] = 'essay';
			$data['min_mark'] = '0';
			$data['name'] = 'Part '.($i+1).' Title';
			$this->save($data);
		}
		
	}
    
    function getNextPart($currentPartid,$batch_id,$type='options')
    {    
        //$this->recursive = -1;
        return $this->find('first',array('conditions'=>array('Part.batch_id'=>$batch_id, /*'Part.type'=>$type,*/ 'Part.status'=>'active','Part.id > '=>$currentPartid)));
    }
    
    function getLastPart($batch_id)
    {
        return $this->find('first',array('conditions'=>array('Part.batch_id'=>$batch_id,'Part.type'=>'essay'),'order'=>array('Part.id desc')));
    }
    
	function getAllParts($batch_id,$type='options')
	{
		if($type=='options'){
			return $this->find('all',array('conditions'=>array('Part.batch_id'=>$batch_id,'Part.type'=>$type)));
		}
		else {
			return $this->find('first',array('conditions'=>array('Part.batch_id'=>$batch_id,'Part.type'=>$type)));
		}		
	}
    
}
